package org.wmine.preprocess;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.JList;

import org.wmine.core.IntegerTextField;
import org.wmine.data.Attribute;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;


public class Generalization extends JFrame implements ActionListener
{
	JLabel labelSelectAtrr,labelClass,lblLabel;
	JButton btnOK,btnGeneralize,btnRight;
	JComboBox cmbAttr;
	//JTextField txtLabel;
	JPanel panel;
	IntegerTextField txtClass;
	JList list1;
	JScrollPane scrLabel,scrItem,scrList;
	JTable tableLabel,tableItem;
	DefaultTableModel dtmLabel,dtmItem;
	Object [][]objLabel;
	Object [][]objItem;
	Vector oo;
	DefaultTableModel dtm;
	static int rowLabel,rowItem;
	//Iterator<Attribute> itratr;
	public Generalization(String [] columnNames, DefaultTableModel dtm)
	{
		
		this.dtm=dtm;
		panel=new JPanel();
		panel.setLayout(null);
		this.setBounds(150,120,600,470);
		this.setTitle("Generalization");
		
		labelSelectAtrr=new JLabel("Select Attribute");
		labelSelectAtrr.setBounds(75,50,100,25);
		
		cmbAttr=new JComboBox();
		for(int i=0;i<columnNames.length;i++)
			cmbAttr.addItem(columnNames[i]);
		cmbAttr.setBounds(215,50,150,25);
		
		labelClass=new JLabel("How Many Classes? ");
		labelClass.setBounds(75,90,150,25);
		
		txtClass=new IntegerTextField();
		txtClass.setBounds(215,90,150,25);
		
		btnOK=new JButton("OK");
		btnOK.setBounds(225,130,100,25);
	    btnOK.addActionListener(this);
		
		panel.add(labelSelectAtrr);
	    panel.add(cmbAttr);
	    panel.add(labelClass);
	    panel.add(txtClass);
	    panel.add(btnOK);
	    
	    
	    panel.repaint();
	    
	    this.add(panel);
		
	}

	public void actionPerformed(ActionEvent ae)
	{
		JButton btn=(JButton)ae.getSource();
		if(btn==btnOK)
		{
			/*lblLabel=new JLabel("Class Label");
			lblLabel.setBounds(10,170,75,25);
			*/
			if(scrItem!=null && scrLabel!=null)
			{
				panel.remove(scrItem);
				panel.remove(scrLabel);
				panel.remove(scrList);
				panel.repaint();
			}
			int num=Integer.parseInt(txtClass.getText());
			
			objLabel=new Object[num][1];
			Object []o1={"Label"};
			dtmLabel=new DefaultTableModel(objLabel,o1);
			tableLabel=new JTable(dtmLabel);
			scrLabel=new JScrollPane(tableLabel);
			scrLabel.setBounds(10,200,150,150);
			dtmLabel.addTableModelListener(new TableModelListener()
			{

				public void tableChanged(TableModelEvent arg0) 
				{
					
					rowLabel=arg0.getFirstRow();
					tableLabel.setEnabled(false);
				}
				
			});
			
			lblLabel=new JLabel("Select Items");
			lblLabel.setBounds(200,170,150,25);
			
			
			
			list1=new JList();
			scrList=new JScrollPane(list1);
			scrList.setBounds(180,200,150,150);
			 panel.add(scrList);
			/*list1.setVisibleRowCount(2);
			 -*/	
			oo=AddItems();
			list1.setListData(oo);
			
			System.out.println(list1.isValid()+ " " +list1.isVisible());
			
			btnRight=new JButton(">>");
			btnRight.setBounds(350,250,50,25);
			btnRight.addActionListener(this);
			
			objItem=new Object[num][1];
			Object []o2={"Selected Items"};
			dtmItem=new DefaultTableModel(objItem,o2);
			tableItem=new JTable(dtmItem);
			scrItem=new JScrollPane(tableItem);
			scrItem.setBounds(420,200,150,150);
			tableItem.setEnabled(false);
			
			btnGeneralize=new JButton("Generalize");
			btnGeneralize.setBounds(225,370,100,25);
			btnGeneralize.addActionListener(this);
			
			panel.add(scrItem);
		    panel.add(scrLabel);
		    panel.add(btnRight);
		    panel.add(lblLabel);
		    panel.add(btnGeneralize);
		    panel.repaint();
		}
		
		if(ae.getSource()==btnRight)
		{
			
			Object []items=list1.getSelectedValues();
			
			
			// forming a string to display in item table separated by ~
			String item="";
			for(int i=0;i<items.length;i++)
			{
				item+=items[i]+"~";
			}
			
			String s=""+dtmItem.getValueAt(rowLabel,0);
			String []ss=s.split("~");
			for(int i=0;i<ss.length;i++)
			{
				if(!(ss[i].trim().equals("")) && !(ss[i].equals("null")) && !(ss[i].equals(null)))
				{
					oo.add(ss[i]);
					
				}
				
			}
			list1.setListData(oo);
			
			tableItem.setValueAt(item,rowLabel,0);
			tableLabel.setEnabled(true);
			
			// remove selected items from list
			Vector lst=new  Vector();
			
			for(int i=0;i<oo.size();i++)
			{
				for(int j=0;j<items.length;j++)
				{
					if(oo.get(i).equals(items[j]))
					{	
						oo.remove(i);
						i--;
						break;	
					}
				}
			}
			System.out.println("oo:"+oo);
			list1.setListData(oo);
			
			
		}
		
		if(ae.getSource()==btnGeneralize)
		{
			int dtmrow=this.dtm.getRowCount();
			Object []obj=new Object[dtmrow];
			Object o="Unknown";
			
			PreprocessPanel.report.append("<br>You chose to perform Generalization<br>");
			PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
			
			for(int j=0;j<dtmrow;j++)
			{
				Object value=this.dtm.getValueAt(j,cmbAttr.getSelectedIndex()); // from original table
				for(int i=0;i<dtmItem.getRowCount();i++)
				{
					
					Object str=dtmLabel.getValueAt(i,0); // get label
					String item=""+dtmItem.getValueAt(i,0); // get items
					String []items=item.split("~");
					int flag=0;
					for(int k=0;k<items.length;k++)
					{	
						if( (value.toString().equals(items[k])))
						{
							obj[j]=str;
							//System.out.println("j::"+j+"  obj[j]::"+obj[j]);
							flag=1;
							break;
						}
					}
					
				}
				
			}
			for(int i=0;i<dtmItem.getRowCount();i++)
			{
				PreprocessPanel.report.append("<br>Items : "+this.dtmItem.getValueAt(i,0)+" Generalized to "+dtmLabel.getValueAt(i,0));
				
				
			}
			PreprocessPanel.report.append("<br>");
			
			for(int i=0;i<obj.length;i++)
			{
				if(obj[i]==null)
					obj[i]="Unknown";
				PreprocessPanel.report.append("<br>Value: "+this.dtm.getValueAt(i,cmbAttr.getSelectedIndex())+" ==> "+obj[i]);
				
				PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
				
			}
			AddColumn(obj,cmbAttr.getSelectedIndex());
			PreprocessPanel.summary.repaint();
			
			
			this.dispose();
		}
	}
	
	Vector AddItems()
	{
		int index=cmbAttr.getSelectedIndex();
		Vector lst=new Vector();
		for(int i=0;i<this.dtm.getRowCount();i++)
		{
			Object o=this.dtm.getValueAt(i,index);
			if(!lst.contains(o))
				lst.add(o);
		}
		//System.out.println("ararylst::"+lst);
		return lst;
	}
	
	void AddColumn(Object [] obj,int i)
	{
		System.gc();
		System.runFinalization();
		this.dtm.addColumn("Class",obj);
		
		Vector v=new Vector();
		v=this.dtm.getDataVector();
		int cnt=this.dtm.getColumnCount();
		//System.out.println("v:"+v+"  :: cnt:: "+cnt);
		
		Vector v1=new Vector(cnt);
		for(int ii=0;ii<cnt;ii++)
			v1.add(this.dtm.getColumnName(ii));
		System.out.println("v1::"+v1);
		v1.removeElementAt(i);
		System.out.println("v1::"+v1);
		
		Iterator itr=v.iterator();
		while(itr.hasNext())
		{
			Vector o=(Vector)itr.next();
			o.removeElementAt(i);
			System.out.println("o::"+o);
		}
		this.dtm.setDataVector(v,v1);
		//System.out.println("after chang v::"+v);
	}
	
	public String getSelectedColumn()
	{
		return cmbAttr.getSelectedItem().toString();
	}
}